package com.sise.stuteaintersys.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sise.stuteaintersys.bean.base.ModifyResponse;
import com.sise.stuteaintersys.bean.entity.*;
import com.sise.stuteaintersys.locallibs.enums.ModifyEnum;
import com.sise.stuteaintersys.mapper.CourseMapper;
import com.sise.stuteaintersys.mapper.CourseTypeMapper;
import com.sise.stuteaintersys.mapper.QuestionMapper;
import com.sise.stuteaintersys.service.AdminCourseTypeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

/**
 * @ClassName AdminCourseTypeServiceImpl
 * @Description TODO
 * @Date 2018/12/28 23:02
 * @Version 1.0
 **/
@Service
@Slf4j
public class AdminCourseTypeServiceImpl implements AdminCourseTypeService {

    @Autowired
    private CourseTypeMapper courseTypeMapper;
    @Autowired
    private QuestionMapper questionMapper;

    @Autowired
    private CourseMapper courseMapper;

    @Override
    public boolean addCourseType(CourseType courseType) {
        if(StringUtils.isEmpty(courseType)){
            log.warn("添加课程分类isEmpty");
            return false;
        }
        try{
            courseType.setGmtCreate(new Date());
            courseType.setGmtModified(new Date());
            int res = courseTypeMapper.insert(courseType);
            return res == 1;
        }catch (Exception e){
            log.warn("错误：{}", e);
            return false;
        }
    }
    @Override
    public ModifyResponse insertQuestion(String list) {
        ModifyResponse modifyResponse = new ModifyResponse();
        List<Question> questionList = JSONArray.parseArray(list, Question.class);
        for (Question question : questionList) {
            question.setGmtModified(new Date());
            question.setGmtCreate(new Date());
        }
        try {
            int number = questionMapper.insertBatch(questionList);
            modifyResponse.setStatus(true);
            modifyResponse.setNumber(number);
            modifyResponse.setMsg(ModifyEnum.QUESTIONADDSUCCESS.getMsg());
            return modifyResponse;
        } catch (Exception e) {
            modifyResponse.setStatus(false);
            modifyResponse.setMsg(ModifyEnum.QUESTIONADDERROR.getMsg());
            return modifyResponse;
        }
    }

    @Override
    public boolean addCourseTypeList(JSONArray list) {
        if(StringUtils.isEmpty(list)){
            log.warn("批量添加课程分类isEmpty");
            return false;
        }
        int count = 0;
        try{
            for(int i = 0; i < list.size(); i++){
                CourseType courseType = new CourseType();
                courseType.setTypeName(list.getString(i));
                courseType.setGmtCreate(new Date());
                courseType.setGmtModified(new Date());
                int res = courseTypeMapper.insert(courseType);
                count = res+count;
            }
            return count == list.size();
        }catch (Exception e){
            log.warn("错误：{}", e);
            return false;
        }
    }

    @Override
    public boolean delCourseType(CourseType courseType) {
        if(StringUtils.isEmpty(courseType)){
            log.warn("删除课程分类isEmpty");
            return false;
        }
        try{
            int res = courseTypeMapper.deleteByPrimaryKey(courseType.getId());
            return res == 1;
        }catch (Exception e){
            log.warn("错误：{}", e);
            return false;
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateCourseType(CourseType courseType) {
        if(StringUtils.isEmpty(courseType)){
            log.warn("更新课程分类isEmpty");
            return false;
        }
        try{
            CourseType type = courseTypeMapper.selectByPrimaryKey(courseType.getId());
            List<Course> courses = courseMapper.selectCourseByCourseTypeName(type.getTypeName());
            int del = courseMapper.deleteByCourseTypeName(type.getTypeName());
            courseTypeMapper.deleteByPrimaryKey(type.getId());
            if(del != courses.size()){
                log.warn("删除数不等于查询出来的数目，可能有问题,courses.size="+courses.size());
            }
            courseType.setGmtCreate(type.getGmtCreate());
            courseType.setGmtModified(new Date());
            courseTypeMapper.insert(courseType);
            int res;

            //尝试使用lambda表达式
            courses.forEach((course) -> course.setTypeName(courseType.getTypeName()));
            res = courses.stream().mapToInt(course -> courseMapper.insert(course)).sum();

            return res == courses.size();
        }catch (Exception e){
            log.warn("错误：{}", e);
            return false;
        }
    }

    @Override
    public CourseType checkCourseType(Integer id) {
        CourseType courseType = courseTypeMapper.selectByPrimaryKey(id);
        if(StringUtils.isEmpty(courseType)){
            log.warn("查询错误，返回值isEmpty");
            return null;
        }
        return courseType;
    }

    @Override
    public PageInfo<CourseType> checkAllCourseType(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<CourseType> list = courseTypeMapper.selectAll();
        return new PageInfo<>(list);
    }

    @Override
    public PageInfo<CourseType> checkAll() {
        CourseTypeExample courseTypeExample = new CourseTypeExample();
        return new PageInfo<>(courseTypeMapper.selectByExample(courseTypeExample));
    }

}
